package twel.probenprophet.helden.heroes.helden5.jaxb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import twel.probenprophet.base.enums.Attribute;

public class AttributeMapAdapter extends XmlAdapter<AttributeMapWrapper, Map<Attribute, Integer>> {

	@Override
	public Map<Attribute, Integer> unmarshal(AttributeMapWrapper attributeList) throws Exception {
		Map<Attribute, Integer> attributeMap = new HashMap<Attribute, Integer>();
		for (AttributeMapEntry listEntry : attributeList.getList()) {
			attributeMap.put(listEntry.getAttribute(), listEntry.getValue());
		}
		return attributeMap;
	}

	@Override
	public AttributeMapWrapper marshal(Map<Attribute, Integer> attributeMap) throws Exception {
		AttributeMapWrapper attributelist = new AttributeMapWrapper();
		List<AttributeMapEntry> list = new ArrayList<AttributeMapEntry>();
		for (Map.Entry<Attribute, Integer> entry : attributeMap.entrySet()) {
			list.add(new AttributeMapEntry(entry.getKey(), entry.getValue()));
		}
		attributelist.setList(list);
		return attributelist;
	}
}
